const app = getApp()
let qCloud = require("../../index.js");

Page({
    /** 页面的初始数据 */
    data: {
        imgPath: qCloud.config.STATIC_IMAGE_PATH,
        tabIndex: 1,
        queryMonth: "",
        wagesTotal: 0, // 工资总额
        issuedWagesTotal: 0, // 已发工资总额
        unpaidWagesTotal: 0, // 未发工资总额
        baseWages: 0, // 底薪
        hourlyWage: 0, // 计时工资
        commission: 0, // 提成
        issuedWages: 0, // 已发工资
        advanceWages: 0, // 预支
        othersWagesTotal: 0, // 其他工资
        wagesOthersList: [], // 其他工资列表
        pieceRateTotal: 0, // 计件工资
        pieceRateList: [], // 计件工资列表
        staffWagesTotal: 0,
        historyWagesList: []
    },

    // tap页选择
    tabTap(e) {
        let self = this
        let tabIndex = e.currentTarget.dataset.index
        if (tabIndex == 1) {
            // 查询当月工资
            self.queryCurrentMonthWages();
        } else {
            // 查询历史工资列表
            self.queryHistoryWagesList();
        }

        self.setData({
            tabIndex: tabIndex,
        });
    },

    // 历史工资详情
    historyDetailTap(e) {
        let self = this
        let queryMonth = e.currentTarget.dataset.query
        self.setData({
            tabIndex: 1,
            queryMonth: queryMonth
        });

        // 查询当月工资
        self.queryCurrentMonthWages();
    },
    /** 生命周期函数--监听页面加载 */
    onLoad: function (options) {
        let self = this;

        // 查询当月工资
        self.queryCurrentMonthWages();
    },
    // 查询当月工资
    queryCurrentMonthWages() {
        let self = this;

        let params = {
            staffId: qCloud.session.getUser().userId,
            queryMonth: self.data.queryMonth,
        };

        app.fetchApis(self, qCloud.api.wages_queryCurrentMonthWages, params, "POST", function (e) {
            let result = e.data;

            if (result.code == 200) {
                let data = result.data;
                // 未发工资总额 = 工资总额 - 已发工资总额
                let unpaidWagesTotal = data.wagesTotal - data.issuedWagesTotal;

                self.setData({
                    queryMonth: data.wagesMonth,
                    wagesTotal: data.wagesTotal,
                    issuedWagesTotal: data.issuedWagesTotal, // 已发工资总额
                    unpaidWagesTotal: Number.parseFloat(unpaidWagesTotal.toFixed(2)), // 未发工资总额
                    baseWages: data.baseWages,
                    hourlyWage: data.hourlyWage,
                    commission: data.commission,
                    issuedWages: data.issuedWages, // 已发工资
                    advanceWages: data.advanceWages, // 预支
                    othersWagesTotal: data.othersWagesTotal,
                    wagesOthersList: data.wagesOthersList,
                    pieceRateTotal: data.pieceRateTotal,
                    pieceRateList: data.pieceRateList,
                });
            }
        })
    },
    // 查询历史工资列表
    queryHistoryWagesList() {
        let self = this;

        let params = {
            staffId: qCloud.session.getUser().userId,
        };

        app.fetchApis(self, qCloud.api.wages_queryHistoryWagesList, params, "POST", function (e) {
            let result = e.data;

            if (result.code == 200) {
                let data = result.data;

                self.setData({
                    historyWagesList: data.historyWagesList,
                    staffWagesTotal: data.staffWagesTotal
                });
            }
        })
    },
    /** 生命周期函数--监听页面初次渲染完成 */
    onReady: function () {
    },
    /** 生命周期函数--监听页面显示 */
    onShow: function () {
    },
    /** 生命周期函数--监听页面隐藏 */
    onHide: function () {
    },
    /** 生命周期函数--监听页面卸载 */
    onUnload: function () {
    },

    /** 页面相关事件处理函数--监听用户下拉动作 */
    onPullDownRefresh: function () {
        let self = this;
        // 查询当月工资
        self.queryCurrentMonthWages();
    },

    /** 页面上拉触底事件的处理函数 */
    onReachBottom: function () {
    },
    /** 用户点击右上角分享 */
    onShareAppMessage: function () {
    }
});
